/*
 * @lc app=leetcode.cn id=844 lang=cpp
 *
 * [844] 比较含退格的字符串
 */

// @lc code=start
class Solution
{
public:
  bool backspaceCompare(string s, string t)
  {
    string S, T;
    for (char& c : s) {
      if (c == '#' && !S.empty())
        S.pop_back();
      else if (c != '#')
        S.push_back(c);
    }

    for (char& c : t) {
      if (c == '#' && !T.empty())
        T.pop_back();
      else if (c != '#')
        T.push_back(c);
    }

    return S == T;
  }
};
// @lc code=end
